//
//  HomefansVideoViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/12.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HomefansVideoViewController.h"
#import "StarInfomationViewController.h"
#import "HomeSearchViewController.h"
#import "FansFocusViewController.h"
#import "StarMovieCell.h"
#import "NSArray+Index.h"
#import "StarInfoModel.h"


@interface HomefansVideoViewController ()<UITableViewDelegate,UITableViewDataSource,StarMovieCellDelegate>{
    NSMutableArray *_OrderArray;
}
@property (nonatomic, strong) UILabel *myLabel;
/**
   我关注的明星总数
 */
@property (nonatomic, strong) UILabel *foucusNumsLabel;
@end

@implementation HomefansVideoViewController

#pragma mark -- 明星列表请求列表
- (void)StarClassListRequest {
    NSDictionary *dict = @{
                           @"udid":User_ID,
                           @"token":TOKEN,
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kStarClass];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        NSArray *array = responseObject[@"data"][@"list"];
        self.foucusNumsLabel.text = KString(responseObject[@"data"][@"subscribe"]);
        for (NSDictionary *dic in array) {
            StarInfoModel *model = [[StarInfoModel alloc] initWithDict:dic];
            [self.dataArr addObject:model];
        }
        _OrderArray = [NSMutableArray arrayWithArray:[NSArray findIndexWithDataArray:self.dataArr]] ;
        [self.tableView reloadData];
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

#pragma mark -- 关注请求
- (void)sendFocusRequestWithFocusId:(NSString *)focusId IndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = @{
                           @"udid":User_ID,
                           @"token":TOKEN,
                           @"follow":focusId
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kFocus];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            self.foucusNumsLabel.text = KString(responseObject[@"data"][@"num"]);
            StarInfoModel *model = _OrderArray[indexPath.section][@"data"][indexPath.row];
            model.booked = responseObject[@"data"][@"status"];
            model.s_booked = KString(responseObject[@"data"][@"number"]);
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    [self setNav];
    [self StarClassListRequest];


}
- (void)setUpUI{
    [self creatTableView];
    
    self.dataArr = [NSMutableArray arrayWithCapacity:1];
    
    self.myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    self.myLabel.textColor = [UIColor whiteColor];
    self.myLabel.center = self.view.center;
    self.myLabel.backgroundColor = kGrayBackGround;
    self.myLabel.layer.cornerRadius = self.myLabel.bounds.size.width * 0.5;
    self.myLabel.layer.masksToBounds = YES;
    self.myLabel.textAlignment = NSTextAlignmentCenter;
    self.myLabel.alpha = 0;
    [self.view addSubview:self.myLabel];
    [self.view bringSubviewToFront:self.myLabel];
    [self.tableView reloadData];
}

- (void)setNav {
    self.navigationItem.title = @"星部落";
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 23, 23)];
    [searchBtn setImage:kSET_IMAGE(@"Search") forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(SearchAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)SearchAction:(UIButton *)sender {
    HomeSearchViewController *searchVC = [[HomeSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT - kNAV_HEIGHT - kTABBAE_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.sectionIndexColor = [UIColor mainBlackColor];
    self.tableView.tableHeaderView = [self creatHeaderView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}

- (UIView *)creatHeaderView {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 55)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(MineFocusAction:)];
    [headerView addGestureRecognizer:tap];
    UIImageView *iconImgView = [[UIImageView alloc] init];
    UILabel *titleLabel = [[UILabel alloc] init];
    UIButton *indcateBtn = [[UIButton alloc] init];
    UILabel *foucusNumsLabel = [[UILabel alloc] init];
    self.foucusNumsLabel = foucusNumsLabel;
    
    titleLabel.font = UIFont(14);
    titleLabel.textColor = [UIColor colorWithHexString:@"333333"];
    foucusNumsLabel.font = UIFont(12);
    foucusNumsLabel.textColor = [UIColor colorWithHexString:@"999999"];

    
    iconImgView.image = kSET_IMAGE(@"living_focus_selected");
    titleLabel.text = @"我的关注";
    foucusNumsLabel.text = @"5";
    [indcateBtn setImage:kSET_IMAGE(@"nextStep") forState:UIControlStateNormal];
    
    
    [headerView addSubview:iconImgView];
    [headerView addSubview:titleLabel];
    [headerView addSubview:indcateBtn];
    [headerView addSubview:foucusNumsLabel];
    
    [iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView.mas_left).offset(13);
        make.height.width.mas_equalTo(35);
        make.centerY.equalTo(headerView.mas_centerY);
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImgView.mas_right).offset(15);
        make.centerY.equalTo(headerView.mas_centerY);
    }];
    
    [indcateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(headerView.mas_right).offset(-20);
        make.centerY.equalTo(headerView.mas_centerY);
    }];
    
    [foucusNumsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(indcateBtn.mas_left).offset(-5);
        make.centerY.equalTo(headerView.mas_centerY);
    }];
    return headerView;
}


#pragma mark -- tableView 代理方法


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if ([_OrderArray[section][@"data"] count] > 0) {
        return _OrderArray[section][@"indexTitle"];
    }
    
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    NSMutableArray *temp = [NSMutableArray array];
    for (NSDictionary *dict in _OrderArray) {
        [temp addObject:dict[@"indexTitle"]];
    }
    
    self.myLabel.text = temp[index];
    
    [UIView animateWithDuration:1.5 animations:^{
        self.myLabel.alpha = 1.0;
    } completion:^(BOOL finished) {
        self.myLabel.alpha = 0;
    }] ;
    return index;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _OrderArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_OrderArray[section][@"data"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StarMovieCell *cell = [StarMovieCell loadNibCellWithTabelView:tableView];
    cell.delegate = self;
    StarInfoModel *model = _OrderArray[indexPath.section][@"data"][indexPath.row];
    cell.infoModel = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    StarInfomationViewController *starInfoVc = [StarInfomationViewController new];
    starInfoVc.hidesBottomBarWhenPushed = YES;
    StarInfoModel *model = _OrderArray[indexPath.section][@"data"][indexPath.row];;
    starInfoVc.s_uid = model.s_udid;
    [self.navigationController pushViewController:starInfoVc animated:YES];
}


//索引的代理方法
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *temp = [NSMutableArray array];
    for (NSDictionary *dict in _OrderArray) {
        [temp addObject:dict[@"indexTitle"]];
    }
    return temp.copy;
}
#pragma mark -- 区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 22)];
    headerView.backgroundColor = [UIColor colorWithHexString:@"EFEFEF"];
    UILabel *label = [[UILabel alloc] init];
    label.font = UIFont(12);
    label.text = _OrderArray[section][@"indexTitle"];
    label.textColor = [UIColor colorWithHexString:@"666666"];
    [headerView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(label.centerY);
        make.left.equalTo(headerView.mas_left).offset(13);
    }];
    return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    NSArray *array = _OrderArray[section][@"data"];
    if (array.count) {
        return 22;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

#pragma mark -- StarMovieDelegate 关注
- (void)focusStar:(StarMovieCell *)cell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    StarInfoModel *model = _OrderArray[indexPath.section][@"data"][indexPath.row];;
    [self sendFocusRequestWithFocusId:KString(model.s_udid)IndexPath:indexPath];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -- 我关注的
- (void)MineFocusAction:(UITapGestureRecognizer *)tap {
    FansFocusViewController *foucsVC = [FansFocusViewController new];
    [self.navigationController pushViewController:foucsVC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
